package com.wwwarehouse.taskcenter.adapter.job_point;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wwwarehouse.common.custom_widget.button.StateButton;
import com.wwwarehouse.common.tools.ConvertUtils;
import com.wwwarehouse.common.tools.ScreenUtils;
import com.wwwarehouse.common.tools.rsa.StringUtils;
import com.wwwarehouse.taskcenter.R;
import com.wwwarehouse.taskcenter.bean.job_point.AddJobPointStep5LookItemMesResponseBean;
import com.wwwarehouse.taskcenter.fragment.job_point.step5.ChooseJobUnitSetnumberDetailFragment5;

import java.util.ArrayList;

/**
 * 定义实物资源的主页面的adapter。
 */

public class TaskPointUnitsetnumberAdapter extends RecyclerView.Adapter<TaskPointUnitsetnumberAdapter.ViewHolder> {
    private ArrayList<AddJobPointStep5LookItemMesResponseBean.ListBean> lists = new ArrayList<>();
    private ChooseJobUnitSetnumberDetailFragment5 fragment5;
    private Context mContext;
    private View view;
    private int itemHeight;

    private void caculateItemHeight() {
        int screenHeight = ScreenUtils.getScreenHeight(mContext);
        int barHeight = ScreenUtils.getStatusHeight(mContext);

        itemHeight = (screenHeight - barHeight - ConvertUtils.dip2px(mContext, 120)) / 10;

    }

    public TaskPointUnitsetnumberAdapter(ArrayList<AddJobPointStep5LookItemMesResponseBean.ListBean> lists, Context mContext, ChooseJobUnitSetnumberDetailFragment5 fragment) {
        this.lists = lists;
        this.mContext = mContext;
        this.fragment5 = fragment;
        caculateItemHeight();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        view = LayoutInflater.from(mContext).inflate(R.layout.item_taskpoint_adapter_relativething_5, parent, false);
        ViewHolder holder = new ViewHolder(view);

        RecyclerView.LayoutParams params = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        params.height = itemHeight;
        view.setLayoutParams(params);


        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        final AddJobPointStep5LookItemMesResponseBean.ListBean bean = lists.get(position);
        holder.itemTitletxt.setText("" + bean.getCategoryName());

        holder.itemStatebtn.setText("x"+ bean.getCount());


        holder.itemStatebtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fragment5.showKeybord(bean.getMetaCategoryUkid());
            }
        });

        if(bean.isJustShow()){


            holder.itemStatebtn.setVisibility(View.GONE);
            holder.itemUnitTxt.setText("" + bean.getCount());
        }else{
            holder.itemStatebtn.setVisibility(View.VISIBLE);

            if(StringUtils.isNullString(bean.getUnitName())){
                holder.itemUnitTxt.setText("");
            }else{
                holder.itemUnitTxt.setText("" + bean.getUnitName());
            }
        }

//        holder.itemRel.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                bean.setClick(!bean.isClick());
//                notifyDataSetChanged();
//                EventBus.getDefault().post(bean);
//            }
//        });

    }

    public void setAdapterNumber(String destUkid , int count){
        for(AddJobPointStep5LookItemMesResponseBean.ListBean bean : lists){
            if(destUkid.equals(bean.getMetaCategoryUkid())){
                bean.setCount(count);
            }
        }

        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return lists.size();
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView itemUnitTxt, itemTitletxt;
        RelativeLayout itemRel;
        StateButton itemStatebtn;

        public ViewHolder(View itemView) {
            super(itemView);
            itemTitletxt = (TextView) itemView.findViewById(R.id.add_poin_unit_adapter_item_txt);
            itemRel = (RelativeLayout) itemView.findViewById(R.id.add_poin_unit_adapter_item_rel);
            itemStatebtn = (StateButton) itemView.findViewById(R.id.add_poin_unit_adapter_statebtn);
            itemUnitTxt = (TextView) itemView.findViewById(R.id.add_poin_unit_adapter_unittxt);
        }
    }


}
